package Layouts 
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.text.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class SelectMenu extends Sprite 
	{
		[Embed(source="../Resources/Texts/kidfont.ttf", fontName = "Kidfont",
		fontStyle = "normal" ,fontWeight = "bold", embedAsCFF= "false")]
		private static var font:Class;
		private var text_format1:TextFormat;
		private var text_format2:TextFormat;
		
		[Embed(source = "../Resources/Images/Paisaje.jpg")]
		private var landscape:Class;
		
		private var nwg_text:TextField;
		private var log_text:TextField;
		private var new_save_name:TextField;
		private var new_save_button_label:TextField;
		private var back_button_label:TextField;
		private var save_name:TextField;
		private var save_level:TextField;
		
		private var par:Main;
		
		private var bg:Sprite = new Sprite();
		private var img_cont:Sprite = new Sprite();
		
		private	var nwg_cont:Sprite = new Sprite();
		private var new_save_cont:Sprite = new Sprite();
		private var new_save_button:Sprite = new Sprite();
		private var back_button:Sprite = new Sprite();
		
		private	var log_cont:Sprite = new Sprite();
		private var save_cont:Sprite = new Sprite();
		
		public function SelectMenu(parent:Main):void
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			par = parent;
		}
		private function init(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			var W:uint = stage.stageWidth;
			var H:uint = stage.stageHeight;
			
			bg.graphics.beginFill(0xFCF089);
			bg.graphics.drawRect(0 , 0, W, H);
			bg.graphics.endFill();
			addChild(bg);
			
			img_cont.graphics.beginFill(0xC99837);
			img_cont.graphics.drawRoundRect(0, 0, W * 10 / 18, H - W / 18,W / 72);
			img_cont.graphics.endFill();
			addChild(img_cont);
			img_cont.x = W / 36;
			img_cont.y = W / 36;
			
			var img:Bitmap = new landscape();
			img_cont.addChild(img);
			img.scaleX = 0.9;
			img.scaleY = 1.05;
			img.x = img_cont.width / 2 - img.width / 2;
			img.y = img_cont.height / 2 - img.height / 2;
			
			text_format1 = new TextFormat();
			text_format1.font = "Kidfont";
			text_format1.size = 20;
			
			nwg_cont.graphics.beginFill(0xC99837);
			nwg_cont.graphics.drawRoundRect(0,0, W * 13 / 36, (H - W / 9) / 5, W / 72);
			nwg_cont.graphics.endFill();
			addChild(nwg_cont);
			nwg_cont.x = W * 11 / 18;
			nwg_cont.y = W / 36;
			
			nwg_text = new TextField();
			nwg_text.embedFonts= true;
			nwg_text.defaultTextFormat = text_format1;
			nwg_text.selectable = false;
			nwg_text.autoSize = TextFieldAutoSize.LEFT;
			nwg_text.text = "Nombre";
			nwg_cont.addChild(nwg_text);
			nwg_text.x = nwg_cont.height/12;
			nwg_text.y = nwg_cont.height/12;
			
			new_save_cont.graphics.beginFill(0xFFE89A);
			new_save_cont.graphics.drawRoundRect(0, 0, W * 13 / 108, (H - W / 9) / 15, W / 108);
			new_save_cont.graphics.endFill();
			nwg_cont.addChild(new_save_cont);
			new_save_cont.x = (H - W / 9) / 15;
			new_save_cont.y = (H - W / 9) / 15;
			
			text_format2 = new TextFormat();
			text_format2.font = "Kidfont";
			text_format2.size = new_save_cont.height * 22 / 24;
			
			new_save_name = new TextField();
			new_save_name.embedFonts= true;
			new_save_name.defaultTextFormat = text_format2;
			new_save_name.type = "input";
			new_save_name.width = new_save_cont.width - new_save_cont.height / 12;
			new_save_name.height = new_save_cont.height * 21 / 24;
			new_save_name.maxChars = 15;
			new_save_cont.addChild(new_save_name);
			new_save_name.x = new_save_cont.height/24;
			new_save_name.y = new_save_cont.height/24;
			
			new_save_button.graphics.beginFill(0xFFE89A);
			new_save_button.graphics.drawRoundRect(0, 0, W * 13 / 108, (H - W / 9) / 15, W / 108);
			new_save_button.graphics.endFill();
			nwg_cont.addChild(new_save_button);
			new_save_button.x = (H - W / 9) * 2/ 15 + new_save_button.width;
			new_save_button.y = (H - W / 9) / 15;
			new_save_button.buttonMode = true;
			new_save_button.useHandCursor = true;
			new_save_button.addEventListener(MouseEvent.CLICK, nwg_click);
			
			new_save_button_label = new TextField();
			new_save_button_label.embedFonts= true;
			new_save_button_label.defaultTextFormat = text_format1;
			new_save_button_label.selectable = false;
			new_save_button_label.autoSize = TextFieldAutoSize.LEFT;
			new_save_button_label.text = "Nuevo Juego";
			new_save_button.addChild(new_save_button_label);
			new_save_button_label.x = new_save_button.width/2 - new_save_button_label.width/2;
			new_save_button_label.y = new_save_button.height/2 - new_save_button_label.height/2;
			
			log_cont.graphics.beginFill(0xC99837);
			log_cont.graphics.drawRoundRect(0, 0, W * 13 / 36, (H - W / 9) * 10 / 15 + W / 36, W / 72);
			log_cont.graphics.endFill();
			addChild(log_cont);
			log_cont.x = W * 22 / 36;
			log_cont.y = W / 12 + (H - W / 9) / 5 - W / 36;
			
			log_text = new TextField();
			log_text.embedFonts= true;
			log_text.defaultTextFormat = text_format1;
			log_text.selectable = false;
			log_text.autoSize = TextFieldAutoSize.LEFT;
			log_text.text = "Cargar Juego";
			log_cont.addChild(log_text);
			log_text.x = nwg_cont.height/12;
			log_text.y = nwg_cont.height / 12;
			
			if (!par.screen_manager.game_files.data.finished_game && par.screen_manager.game_files.data.save_name != null) {
				
				save_cont.graphics.beginFill(0xFFE89A);
				save_cont.graphics.drawRoundRect(0,0, log_cont.width * 16/ 18, (H - W / 9) / 6, W / 72);
				save_cont.graphics.endFill();
				log_cont.addChild(save_cont);
				save_cont.x = log_cont.width / 18;
				save_cont.y = log_cont.width * 3 / 18;
				save_cont.buttonMode = true;
				save_cont.useHandCursor = true;
				save_cont.addEventListener(MouseEvent.CLICK, saved_game_click);				
				
				save_name = new TextField();
				save_name.embedFonts = true;
				save_name.defaultTextFormat = text_format2;
				save_name.selectable = false;
				save_name.autoSize = TextFieldAutoSize.LEFT;
				save_name.text = "Partida : " + par.screen_manager.game_files.data.save_name;
				save_cont.addChild(save_name);
				save_name.x = save_cont.width / 36;
				save_name.y = save_cont.width / 36;
				
				save_level = new TextField();
				save_level.embedFonts = true;
				save_level.defaultTextFormat = text_format2;
				save_level.selectable = false;
				save_level.autoSize = TextFieldAutoSize.LEFT;
				save_level.text = "Nivel : " + par.screen_manager.game_files.data.save_level;
				save_cont.addChild(save_level);
				save_level.x = save_cont.width / 36;
				save_level.y = save_cont.width / 18 + save_name.height;
				
			}
			
			back_button.graphics.beginFill(0xC99837);
			back_button.graphics.drawRoundRect(0,0, W * 13 / 36, H - (nwg_cont.height + log_cont.height + W/9), W / 72);
			back_button.graphics.endFill();
			addChild(back_button);
			back_button.x = W * 11 / 18;
			back_button.y = W / 12 + nwg_cont.height + log_cont.height;
			back_button.buttonMode = true;
			back_button.useHandCursor = true;
			back_button.addEventListener(MouseEvent.CLICK, back_click);
			
			back_button_label = new TextField();
			back_button_label.embedFonts= true;
			back_button_label.defaultTextFormat = text_format1;
			back_button_label.selectable = false;
			back_button_label.autoSize = TextFieldAutoSize.LEFT;
			back_button_label.text = "Regresar";
			back_button.addChild(back_button_label);
			back_button_label.x = back_button.width / 2 - back_button_label.width / 2;
			back_button_label.y = back_button.height / 2 - back_button_label.height / 2;
			
		}
		
		private function nwg_click(e:MouseEvent):void {
			if (new_save_name.text != "") {
				par.screen_manager.game_files.data.save_level = 1;
				par.screen_manager.game_files.data.save_name = new_save_name.text;
				par.screen_manager.goToScreen("Game")
				par.screen_manager.game_files.data.finished_game = false;
			}		
		}
		
		private function saved_game_click(e:MouseEvent):void {
			par.screen_manager.goToScreen("Game");
		}
		
		private function back_click(e:MouseEvent):void {
			par.screen_manager.goToScreen("MainMenu")
		}
		
	}

}